<?php
/**
 * 周舟 hzboye010@163.com
 * addby sublime snippets
 */
namespace app\shop\api;
use app\common\api\BaseApi;
use app\shop\api\ProductSkuApi;
use app\shop\model\ProductPackage;
use app\shop\model\ProductPackageItem;

class ProductPackageApi extends BaseApi{

	const ADD      = "shop/ProductPackageApi/add";
	const SAVE     = "shop/ProductPackageApi/save";
	const GET_INFO = "shop/ProductPackageApi/getInfo";
	const SET_INC  = "shop/ProductPackageApi/setInc";

	/*private*/
	const PACKAGE_DETAIL = "shop/ProductPackageApi/packageDetail";


	protected function _init(){
		$this->model = new ProductPackage();
	}

	public function packageDetail($id){
		$r = $this->getInfo([['id'=>$id]]);
		if(!$r['status']) return $this->apiReturnErr($r['info']);
		$pInfo = $r['info'];
		if(empty($pInfo)) return $this->apiReturnErr('套餐ID错误');
		if($pInfo['end_time'] < time()) return $this->apiReturnErr('套餐已过期');
		$itemModel = new ProductPackageItem();
		$res = $itemModel ->alias('pi')
		->join('left join itboye_product as p on p.id=pi.p_id')
		->join('left join itboye_product_image as i on i.pid=pi.p_id and i.type=6015')
		->where(['pi.package_id'=>$id])->field('pi.*,p.name,p.onshelf,p.status,i.img_id as main_img')->select();
		if(false === $res) return $this->apiReturnErr($itemModel->getDbError());
		foreach ($res as &$v) {
			$skuid = intval($v['sku_id']);
			$pid   = $v['p_id'];
			$v['psku_id'] = ''.$skuid;
			if($skuid){
				//skuid<>0 查询单个规格
				$r = apiCall(ProductSkuApi::GET_INFO,[['id'=>$skuid,'product_id'=>$pid],false,'id,sku_id,sku_desc,product_code,icon_url']);
				if(!$r['status']) return $this->apiReturnErr($r['info']);
				$v['sku_info'] = empty($r['info']) ? ['temp'=>'']: $r['info'];
				$v['has_sku']  = '0';
				$v['sku_id']   = [];
				$v['sku_name'] = [];
				$v['sku_list'] = [];
				unset($v['sku_id']);
			}else{
				//skuid=0 查询所有规格
				$rs = apiCall(ProductSkuApi::QUERY_NO_PAGING,[['product_id'=>$pid],false,'id,sku_id,sku_desc,product_code,icon_url']);
				if(!$rs['status']) return $this->apiReturnErr($rs['info']);
				if(empty($rs['info'])) return $this->apiReturnErr('该商品还未添加规格');
				$multi_sku = count($rs['info'])==1 ? 0 :1;
				$v['has_sku']  = ''.$multi_sku;
				if($multi_sku){
					//规格id
          $r = apiCall(ProductSkuApi::GET_SKU_ID,[$pid,'sku_id']);
          if($r['status']){
              $v['sku_id'] = json_decode($r['info']);
          }else{
              return $this -> apiReturnErr($r['info']);
          }
          //规格名称
          $r = apiCall(ProductSkuApi::GET_SKU_NAME,[$pid,'sku_desc']);
          if($r['status']){
              $v['sku_name'] = json_decode($r['info']);
          }else {
              return $this->apiReturnErr($r['info']);
          }
					$v['sku_list'] = $rs['info'];
					$v['sku_info'] = ['temp'=>''];
				}else{
					$v['sku_id']   = [];
					$v['sku_name'] = [];
					$v['sku_list'] = [];
					$v['sku_info'] = empty($rs['info'][0]) ? ['temp'=>'']:$rs['info'][0];
				}
			}
		}
		$pInfo['items'] = $res;
		return $this->apiReturnSuc($pInfo);
	}
}